<?php

/**
 * 开放商户 - 微信实名认证查询功能使用示例
 */

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Openmerch\Wechat\WechatRealnameQueryService;
use JLPay\Models\Openmerch\Wechat\RealnameWxQueryRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

// 设置错误报告
error_reporting(E_ALL);
ini_set('display_errors', 1);

// =================== 日志配置 =====================
$logger = Logger::getInstance();

// =================== 工具函数 =====================
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
function buildRealnameWxQueryRequest(): RealnameWxQueryRequest
{
    $request = new RealnameWxQueryRequest();
    
    // 设置请求参数
    $request->setMerchNo("849584358140020");
    $request->setQueryType("0");  // 申请单查询
    
    return $request;
}

// =================== 示例函数 =====================
function wechatRealnameQueryDemo()
{
    global $logger;
    
    try {
        $logger->info("开始执行微信实名认证查询示例");
        
        // 创建配置对象和微信实名认证查询服务
        $config = createConfig();
        $wechatRealnameQueryService = new WechatRealnameQueryService($config);
        
        // 创建微信实名认证查询请求
        $request = buildRealnameWxQueryRequest();
        
        // 发送请求 - 使用DemoRequestHandler
        $response = DemoRequestHandler::sendRequestAndHandleResponse(
            $wechatRealnameQueryService, 
            $request, 
            "微信实名认证查询",
            function($service, $request) {
                return $service->queryRealnameWx($request);
            }
        );
        
        // 处理响应结果
        if ($response && $response->isSuccess()) {
            $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        }
        
        $logger->info("微信实名认证查询示例执行完毕");
    } catch (Exception $e) {
        $logger->error("示例执行异常: " . $e->getMessage());
        echo "Error: " . $e->getMessage() . "\n";
        echo "Stack trace: " . $e->getTraceAsString() . "\n";
    }
}

// =================== 主程序 =====================
try {
    $logger->info("===================================================");
    $logger->info("===== 微信实名认证查询示例 开始 =====");
    $logger->info("===================================================");

    wechatRealnameQueryDemo();

    $logger->info("===================================================");
    $logger->info("===== 微信实名认证查询示例执行完毕 =====");
    $logger->info("===================================================");
} catch (Exception $e) {
    echo "Fatal Error: " . $e->getMessage() . "\n";
    echo "Stack trace: " . $e->getTraceAsString() . "\n";
} 